package com.wayne.routers;

import com.wayne.handlers.UserHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
import static org.springframework.http.MediaType.TEXT_EVENT_STREAM;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@Configuration
public class AllRouters {
    @Bean
    public RouterFunction<ServerResponse> userRouter(UserHandler handler){
        return nest(
                //相当于类上面的@RequestMapping("/user")
                path("/user"),
                // 相当于类方法里面的@GetMapping
                route(GET(""),handler::getAllUser)
                        .andRoute(GET("/stream"),handler::getStreamAllUser)
                        .andRoute(POST("").and(accept(APPLICATION_JSON_UTF8)),handler::createUser)
                        .andRoute(DELETE("/{id}"),handler::deleteUser)
                        .andRoute(PUT("/{id}").and(accept(APPLICATION_JSON_UTF8)),handler::updateUser)
                        .andRoute(GET("/{id}"),handler::findUserById));
    }
}
